//
//  CGQdetailCell.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/4/7.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import YYText

class CGQdetailCell: SYBaseCollectionViewCell {
    
    public var passParamsClosure: ((Any) -> Void)?
    
    var paramsList: [NSControlParamsListModel]!
    
    var bgView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.white
        v.layer.cornerRadius = 5.autoWidth()
        v.layer.masksToBounds = true
        v.clipsToBounds = true
        return v
    }()
    
    var topBgView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xE6F2FF)
        return v
    }()
    
    let biaohao: UILabel = {
        let v = UILabel()
        v.text = "180807000001-0001-01"
        v.font = 12.autoFontSize()
        v.textColor = UIColor(hex: 0x333333)
        v.textAlignment = .center
        return v
    }()
    
    let title: UILabel = {
        let v = UILabel()
        v.text = "空气温度"
        v.font = 15.autoFontSize()
        v.textColor = UIColor(hex: 0x5C7D9C)
        v.textAlignment = .center
        return v
    }()
    
    let num: YYLabel = {
        let v:YYLabel = YYLabel()
        return v
    }()
    
    var kai: UISegmentedControl = {
        var v = UISegmentedControl()
        let items = ["开","关"] as [Any]
        
        
        v.setTitleTextAttributes([NSAttributedString.Key.font :UIFont.systemFont(ofSize: 14.autoWidth())], for: .normal)
        if #available(iOS 13.0, *) {
          v.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)

          v.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(hex: 0x08cc81)], for: .normal)

            v.selectedSegmentTintColor = UIColor(hex: 0x08CC81)


        } else {
            // Fallback on earlier versions
        }
        v = UISegmentedControl(items:items)
//        v.removeBorders()

        return v
    }()
    

    
    lazy var edit: VGBaseTitleImgView = {
        let v: VGBaseTitleImgView = VGBaseTitleImgView(frame: CGRect.zero,showType: .normal)
        v.textLb.text = "编辑"
        v.textLb.textColor = UIColor(hex: 0xA666666)
        v.textLb.font = 13.autoFontSize()
        v.imageV.image = UIImage(named: "detail_edit")
//        v.tapClosure = {[weak self] (index, isSelect) in
//            if let weakSelf = self {
//
//                self?.passParamsClosure!("bianji")
//            }
//        }
        return v
    }()

    
    var sepView: UIView = {
        let v = UIView()
        v.backgroundColor = Constant.bgViewColor
        v.isHidden = true
        return v
    }()
    
    let selectImg: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "detail_select")
        return v
    }()
    
    func configure(model:NSDataListModel)  {
        biaohao.text = model.deviceId
        title.text = model.deviceName
        kai.removeAllSegments()
    
        // MARK: - map 对数组中每个元素进行操作并返回新的元素  filter 是以{}中的条件对数组进行筛选并返回原类型数组
//        let a: [String] = model.controlParamsList?.map{$0.value ?? ""} ?? []

        self.paramsList = model.controlParamsList ?? []
        for (i, value) in self.paramsList.enumerated()  {

            
            
            if value.text == "空" {
                
                
            }else{
                kai.insertSegment(withTitle: value.text, at: i, animated: false)
                
                if value.text == model.textValue {
                    kai.selectedSegmentIndex = i
                }
            }
           
            
        }
        kai.addTarget(self, action: #selector(segmentDidchange), for: .valueChanged) //添加值改变监听


        if model.isControllable == 1 {
            
            num.isHidden = true
            kai.isHidden = false

        }else{
            num.isHidden = false
            kai.isHidden = true
        }
        
        if model.isSelect {
            selectImg.image = UIImage(named: "detail_select")

        }else{
            selectImg.image = UIImage(named: "detail_noselect")

        }
        
       
        
        let firStr = model.textValue ?? ""
        let totalStr = String(format: "%@%@",firStr,model.unit ?? "")
        let attString = NSMutableAttributedString(string: totalStr)
        attString.yy_setColor(UIColor(hex: 0x0782FF), range: NSRange(location: 0, length: firStr.count))
        attString.yy_setFont(27.autoFontSize(), range: NSRange(location: 0, length:firStr.count))
        attString.yy_setColor(UIColor(hex: 0x0782FF), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        attString.yy_setFont(15.autoFontSize(), range: NSRange(location: firStr.count, length: totalStr.count - firStr.count))
        attString.yy_setAlignment(.center, range: NSRange(location: 0, length: totalStr.count))
        
        num.attributedText = attString
        
        
        edit.tapClosure = {[weak self] (index, isSelect) in
            if self != nil {
                
                self?.passParamsClosure!(("bianji", NSControlParamsListModel()))

            }
        }
        
    }
//    
    @objc func segmentDidchange(_ segmented:UISegmentedControl)  {
        
        print(segmented.selectedSegmentIndex)
    
        passParamsClosure!(("xuanze", self.paramsList[segmented.selectedSegmentIndex]))
        
      
    }
    
    override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        
        self.setNeedsLayout()
        self.layoutIfNeeded()
        
       let  size =  self.contentView.systemLayoutSizeFitting(layoutAttributes.size)
        var cellFrame = layoutAttributes.frame
        cellFrame.size.height = size.height
        layoutAttributes.frame = cellFrame
        return layoutAttributes
        
        
    }
    

    
    override func initViews() {
        num.isHidden = true
       
        
        self.backgroundColor = .clear
        addSubview(bgView)
        bgView.addSubview(topBgView)
        addSubview(biaohao)
        addSubview(title)
       contentView.addSubview(edit)
//        addSubview(delete)
        addSubview(num)
        addSubview(lineView)
        addSubview(sepView)
        
        addSubview(kai)
        addSubview(selectImg)
        
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        topBgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(40.autoWidth())
        }
        selectImg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(13.autoWidth())
            make.left.equalToSuperview().offset(8.autoWidth())
            make.width.height.equalTo(14.autoWidth())
        }
        biaohao.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.autoWidth())
            make.left.equalTo(selectImg.snp.right).offset(2.autoWidth())
            make.right.equalToSuperview()
            make.height.equalTo(10.autoWidth())
        }
        title.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(60.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(15.autoWidth())
        }
        num.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(90.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(22.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-39.autoWidth())
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5.autoWidth())
        }
        edit.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(132.autoWidth())
            make.left.equalToSuperview().offset(25.autoWidth())
            make.right.equalToSuperview().offset(-25.autoWidth())
            make.height.equalTo(37.autoWidth())
        }
//        delete.snp.makeConstraints { (make) in
//            make.top.equalToSuperview().offset(132.autoWidth())
//            make.left.equalToSuperview().offset(101.autoWidth())
//            make.right.equalToSuperview().offset(-25.autoWidth())
//            make.height.equalTo(37.autoWidth())
//        }
        sepView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(132.autoWidth())
            make.left.equalToSuperview().offset(86.autoWidth())
            make.width.equalTo(0.5.autoWidth())
            make.height.equalTo(17.autoWidth())
        }
        
        kai.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(90.autoWidth())
            make.left.equalToSuperview().offset(27.autoWidth())
            make.height.equalTo(25.autoWidth())
            make.right.equalToSuperview().offset(-27.autoWidth())
        }

    }

}


class CGQdetailBottomView: SYBaseView {
    
    
    let imgView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(named: "detail_noselect")
        v.layer.cornerRadius = 9.autoWidth()
        v.layer.masksToBounds = true
        v.layer.borderWidth = 0.5
        v.layer.borderColor = UIColor(hex: 0x0782FF).cgColor
        return v
    }()
    
    let tipText: UILabel = {
        let v = UILabel()
        v.text = "全选"
        v.font = 13.autoFontSize()
        v.textColor = UIColor(hex: 0x323232)
        return v
    }()
    
    var quanxuanBt: UIButton = {
        let v = UIButton()
        return v
    }()
    
    var tixianBt: UIButton = {
        let v = UIButton()
        v.setTitle("一键禁用", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 17.autoFontSize()
        v.layer.cornerRadius = 20.autoWidth()
        v.backgroundColor = UIColor(hex: 0xC2C2C2)
        return v
    }()
    
    override func initViews() {
        self.backgroundColor = .white
        addSubview(imgView)
        addSubview(tipText)
        addSubview(tixianBt)
        addSubview(quanxuanBt)
        imgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(19.autoWidth())
            make.left.equalToSuperview().offset(10.autoWidth())
            make.width.equalTo(18.autoWidth())
            make.height.equalTo(18.autoWidth())
        }
        tipText.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(20.autoWidth())
            make.left.equalToSuperview().offset(35.autoWidth())
            make.height.equalTo(16.autoWidth())
        }
        quanxuanBt.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(20.autoWidth())
            make.left.equalToSuperview()
            make.height.equalTo(20.autoWidth())
            make.width.equalTo(120.autoWidth())
        }
        tixianBt.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(7.autoWidth())
            make.right.equalToSuperview().offset(-10.autoWidth())
            make.width.equalTo(120.autoWidth())
            make.height.equalTo(40.autoWidth())
        }
    }
}


extension UISegmentedControl {
    func removeBorders() {
        setBackgroundImage(imageWithColor(color: UIColor.clear), for: .normal, barMetrics: .default)
        setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default)
        setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
    }

    // create a 1x1 image with this color
    private func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width:  1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor);
        context!.fill(rect);
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image!
    }
}
